package cn.learn.service.cargo.impl;

import cn.learn.dao.cargo.ContractDao;
import cn.learn.dao.cargo.ExtCproductDao;
import cn.learn.domain.cargo.Contract;
import cn.learn.domain.cargo.ExtCproduct;
import cn.learn.domain.cargo.ExtCproductExample;
import cn.learn.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;

    @Override
    public void save(ExtCproduct extCproduct) {
        // 设置编号
        extCproduct.setId(UUID.randomUUID().toString().replace("-", ""));
        // 设置总金额
        Double total = extCproduct.getPrice() * extCproduct.getCnumber();
        extCproduct.setAmount(total);
        // 保存附件
        extCproductDao.insertSelective(extCproduct);
        // 获取合同
        System.out.println(extCproduct.getContractId());
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        // 修改总金额
        contract.setTotalAmount(contract.getTotalAmount() + total);
        // 修改合同附件数
        contract.setExtNum(contract.getExtNum() + 1);
        // 修改合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ExtCproduct extCproduct) {
        // 获取源数据
        ExtCproduct oldData = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        // 获取老数据金额
        Double old_total = oldData.getAmount();
        // 计算新数据并赋值
        Double new_total = extCproduct.getPrice() * extCproduct.getCnumber();
        extCproduct.setAmount(new_total);
        // 修改货物
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
        // 获取合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        // 修改金额
        contract.setTotalAmount(contract.getTotalAmount() - old_total + new_total);
        // 修改合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
        // 货物附件
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        // 获取附件总金额
        Double total = extCproduct.getAmount();
        // 删除附件
        extCproductDao.deleteByPrimaryKey(id);
        // 获取合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());

        System.out.println(contract);
        // 修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount() - total);
        // 修改合同的附件数量
        contract.setExtNum(contract.getExtNum() - 1);
        // 修改合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ExtCproduct findById(String id) {
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        return extCproduct;
    }

    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(example);
        PageInfo pageInfo = new PageInfo(extCproducts);
        return pageInfo;
    }
}
